﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
//9.通过AssetReference加载的资源如何释放
public class Example2 : MonoBehaviour
{
    //GameObject
    public GameObject cubePrefab;
    private GameObject m_cubeInstance;

    //AssetReference
    public AssetReference cubeRef;
    private GameObject m_cubeRefInstance;

    //AssetReferenceTexture
    public AssetReferenceTexture texRef;
    private Texture m_texLoadedObject;
    //scene
    public AssetReference sceneRef;
    private void Start()
    {
        
    }
    private void OnGUI()
    {
        ///////////////////////////////////////传统方式//////////////////////////////////////////
        if (GUILayout.Button("传统使用Prefab 实例化"))
        {
            m_cubeInstance = Instantiate(cubePrefab);
        }

        if (GUILayout.Button("传统使用Prefab 释放"))
        {
            if(m_cubeInstance!=null)
                Destroy(m_cubeInstance);
        }
        //////////////////////////////////////////AssetReference///////////////////////////////////////
        if (GUILayout.Button("AssetReference  加载"))
        {
            cubeRef.InstantiateAsync().Completed += args =>
            {
                Debug.Log("加载" + args.Result.name);
                m_cubeRefInstance = args.Result;
            };
        }

        if (GUILayout.Button("AssetReference 销毁"))
        {
            //Destroy(_cubeInstance);虽然能销毁，但是
            cubeRef.ReleaseInstance(m_cubeRefInstance);//内部使用引有计算器
        }

        //////////////////////////////////////////Texture///////////////////////////////////////
        /* 操作步骤
         * 1. cubeRef 加载 
         * 2.texture加载 
         * 3.texture释放【发现场景找那个贴图在cube上】
         * 4.cubeRef释放【贴图也是随即释放】
         */
        if (GUILayout.Button("AssetReferenceTexture  加载"))//贴图没有实例化，只有加载，那怎么释放呢
        {
            texRef.LoadAssetAsync().Completed += args =>
            {
                Debug.Log("加载" + args.Result.name);
                m_texLoadedObject = args.Result;
                m_cubeRefInstance.GetComponent<Renderer>().material.mainTexture = m_texLoadedObject;
            };
        }

        if (GUILayout.Button("AssetReferenceTexture 卸载"))//贴图的卸载
        {
            texRef.ReleaseAsset();
        }
        //////////////////////////////////////////Scene///////////////////////////////////////
        
        if (GUILayout.Button("Scene  加载"))//
        {
            //加载一个场景
            sceneRef.LoadSceneAsync().Completed += args =>
            {
                Debug.Log("加载" + args.Result.Scene.name);
            };
        }

        if (GUILayout.Button("Scene  卸载"))//
        {
            //在当前场景下，加载其他场景，当前场景会自动释放掉
            UnityEngine.SceneManagement.SceneManager.LoadScene("");
        }

    }


    private void Loaded(AsyncOperationHandle<GameObject> obj)
    {
        var cube = obj.Result;
        Debug.Log(cube.name);

        cube.GetComponent<Renderer>().material.mainTexture = texRef.Asset as Texture;
    }


}
